package com.youbug.mall.product_pms.service.impl;

import com.youbug.mall.product_pms.entity.PmsCategoryBrandRelation;
import com.youbug.mall.product_pms.mapper.PmsCategoryBrandRelationMapper;
import com.youbug.mall.product_pms.service.IPmsCategoryBrandRelationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
import com.youbug.common.vo.QueryVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.util.StringUtils;

import java.util.Collection;

/**
 * <p>
 * 品牌分类关联 服务实现类
 * </p>
 *
 * @author youbug
 * @since 2024-03-31
 */
@Service
public class PmsCategoryBrandRelationServiceImpl extends ServiceImpl<PmsCategoryBrandRelationMapper, PmsCategoryBrandRelation> implements IPmsCategoryBrandRelationService {

        @Override
        public IPage<PmsCategoryBrandRelation> getPmsCategoryBrandRelationPage(QueryVo<PmsCategoryBrandRelation> queryVo) {

        Page<PmsCategoryBrandRelation> paramPage = new Page<>();

        paramPage.setCurrent(queryVo.getCurrentPage());

        paramPage.setSize(queryVo.getPageSize());

        QueryWrapper<PmsCategoryBrandRelation> wrapper = new QueryWrapper<>();

        PmsCategoryBrandRelation pmsCategoryBrandRelation = queryVo.getPayload();

        if(!ObjectUtils.isEmpty(pmsCategoryBrandRelation)){
            if(StringUtils.hasText(pmsCategoryBrandRelation.getBrandName())){
                wrapper.eq("brand_name", pmsCategoryBrandRelation.getBrandName());
            }
            if(StringUtils.hasText(pmsCategoryBrandRelation.getCatalogName())){
                wrapper.eq("catalog_name", pmsCategoryBrandRelation.getCatalogName());
            }
            if(!ObjectUtils.isEmpty(pmsCategoryBrandRelation.getBrandId())){
                wrapper.eq("brand_id", pmsCategoryBrandRelation.getBrandId());
            }
        }
        if(!ObjectUtils.isEmpty(queryVo.getStartTime())){
            wrapper.gt("create_time", queryVo.getStartTime());
        }
        if(!ObjectUtils.isEmpty(queryVo.getEndTime())){
            wrapper.lt("create_time", queryVo.getEndTime());
        }
        wrapper.orderByDesc("create_time");

        Page<PmsCategoryBrandRelation> page = this.page(paramPage, wrapper);

        return page;
        }

    @Transactional(transactionManager = "transactionManagerPms", rollbackFor = Exception.class)
    public boolean saveBatch(Collection<PmsCategoryBrandRelation> entityList) {
        return saveBatch(entityList, DEFAULT_BATCH_SIZE);
    }

    @Override
    public boolean updateBrand(Long brandId, String name) {
        QueryWrapper<PmsCategoryBrandRelation> wrapper = new QueryWrapper<>();
        wrapper.eq("brand_id", brandId);
        PmsCategoryBrandRelation relation = new PmsCategoryBrandRelation();
        relation.setBrandName(name);
        return this.update(relation, wrapper);
    }

    @Override
    public boolean updateCategory(Long catId, String name) {
        QueryWrapper<PmsCategoryBrandRelation> wrapper = new QueryWrapper<>();
        wrapper.eq("catalog_id", catId);
        PmsCategoryBrandRelation relation = new PmsCategoryBrandRelation();
        relation.setCatalogName(name);
        return this.update(relation, wrapper);
    }

}
